package com.ericsson.enap.modules.ossalarm.thread;

import com.ericsson.enap.modules.sys.entity.EnipNetelement;
import org.slf4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Queue;

/**
 * 读取ENM告警信息的线程
 * @author yh
 */
public class OssReadAlarmThread extends Thread{
    private Queue msgQueue;
    private Logger log;
    private Map<String, EnipNetelement> netElementMap;
    public OssReadAlarmThread(Map<String,EnipNetelement> netElementMap, Queue e, Logger log){
        this.netElementMap=netElementMap;
        this.msgQueue=e;
        this.log=log;
    }
    @Override
    public void run() {
        StringBuilder sb=new StringBuilder();
        Process proc = null;
        BufferedReader br;
        BufferedReader brError;
        String line;
        String lineError;
        try {
            String[] cmd = new String[3];
            cmd[0] = "sh";
            cmd[1] = "-c";
            cmd[2] = "/opt/Ericsson/core/bin/ossImsAlarm.sh";
            try {
                proc = Runtime.getRuntime().exec(cmd);
            } catch (IOException e) {
                log.error("IOException readAlarm cmd"+e.getMessage());
            }
            br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
            brError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
            try {
                //用于判断告警的网元类型是否为IMS,1:是，0:不是
                int remarkNetType=0;
                int start=0;
                while ((line = br.readLine()) != null) {
                    if(line.indexOf("%a")!=-1){
                        start=1;
                    }
                    if(start==1) {
                        sb.append(line + "\n");
                        if (line.contains("-Fdn=NetworkElement=")) {
                            String netName = line.substring(20, line.length());
                            log.info(netName);
                            if (this.netElementMap.containsKey(netName)) {
                                remarkNetType = 1;
                            }
                        }
                        if (sb.indexOf("%a") != -1 && sb.indexOf("%A") != -1 && remarkNetType == 1) {
                            log.info("______________________________" + sb.toString() + "______________________________________");
                            msgQueue.offer(sb.toString());
                            remarkNetType = 0;
                            sb.delete(0, sb.length());
                        } else if (sb.indexOf("%a") != -1 && sb.indexOf("%A") != -1 && remarkNetType == 0) {
                            //log.info("++++++++++++++++++++++++++++++"+sb.toString()+"++++++++++++++++++++++++++++++++++++++");
                            sb.delete(0, sb.length());
                        }
                    }
                }
                while ((lineError = brError.readLine()) != null) {
                    sb.append(lineError);
                }
                proc.waitFor();
                log.error("proc end"+sb.toString());
            } catch (IOException e) {
                log.error("IOException readAlarm"+e.getMessage());
            }
        } catch (Exception e) {
            log.error("Exception readAlarm"+e.getMessage());
        } finally {
            proc.destroy();
        }
    }
}
